package top.college.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JTextField;

import top.college.dao.JDBC_ConnClose;
import top.college.dao.JDBC_SQL;
import top.college.model.AdminVO;
import top.college.screen.LoginMain;

public class AdminRead {
	public static Connection conn = null;
	public static PreparedStatement pstmt = null;
	public static ResultSet rs = null;
	
	public static List<AdminVO> readAdmin() {
		List<AdminVO> result = new ArrayList<>();	
			try {
				conn = JDBC_ConnClose.openDataBase();
				pstmt = conn.prepareStatement( JDBC_SQL.AdminSelectAll1SQL );
				rs = pstmt.executeQuery();
				while( rs.next() ) {
					AdminVO m = new AdminVO();
					m.setAdmin_id( rs.getString("ADMIN_ID") );
					m.setAdmin_name( rs.getString("ADMIN_NAME") );
					m.setAdmin_tel( rs.getString("ADMIN_TEL") );
					m.setReg_date( rs.getString("REG_DATE") );
					result.add( m );
				}
			} catch ( Exception ex ) {
				ex.printStackTrace();
			}finally {
				JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
			}
			return result;
		}
	
	public static List<AdminVO> readAdminName(JTextField Name) {
		List<AdminVO> result = new ArrayList<>();	
			try {
				conn = JDBC_ConnClose.openDataBase();
				
				pstmt = conn.prepareStatement( JDBC_SQL.AdminSelectAll3SQL );
				
				pstmt.setString( 1, Name.getText() );
				rs = pstmt.executeQuery();
				while( rs.next() ) {
					AdminVO m = new AdminVO();
					m.setAdmin_id( rs.getString("ADMIN_ID") );
					m.setAdmin_name( rs.getString("ADMIN_NAME") );
					m.setAdmin_tel( rs.getString("ADMIN_TEL") );
					m.setReg_date( rs.getString("REG_DATE") );
					result.add( m );
				}
			} catch ( Exception ex ) {
				ex.printStackTrace();
			}finally {
				JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
			}
			return result;
		}
	
	public static List<AdminVO> readAdminId(JTextField IdText) {
		List<AdminVO> result = new ArrayList<>();	
			try {
				conn = JDBC_ConnClose.openDataBase();
				pstmt = conn.prepareStatement( JDBC_SQL.AdminSelectAll2SQL );
				pstmt.setString( 1, IdText.getText() );
				rs = pstmt.executeQuery();
				while( rs.next() ) {
					AdminVO m = new AdminVO();
					m.setAdmin_id( rs.getString("ADMIN_ID") );
					m.setAdmin_name( rs.getString("ADMIN_NAME") );
					m.setAdmin_tel( rs.getString("ADMIN_TEL") );
					m.setReg_date( rs.getString("REG_DATE") );
					result.add( m );
				}
			} catch ( Exception ex ) {
				ex.printStackTrace();
			}finally {
				JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
			}
			return result;
		}
	
	public static AdminVO readAdmin( String AdminID ) {
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.AdminSelectAll4SQL);
			pstmt.setString( 1, AdminID );
			rs = pstmt.executeQuery();
			while ( rs.next() ) {
				AdminVO av = new AdminVO();
				av.setAdmin_id( rs.getString("ADMIN_ID") );
				av.setAdmin_name( rs.getString("ADMIN_NAME") );
				av.setAdmin_pw(rs.getString("ADMIN_PW"));
				av.setAdmin_tel( rs.getString("ADMIN_TEL") );
				av.setReg_date( rs.getString("REG_DATE") );
				return av;
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally { 
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return null;
	}
}